package org.yanfengying.prod.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.yanfengying.prod.domain.ProductType;
import org.yanfengying.prod.mapper.ProductTypeMapper;
import org.yanfengying.prod.service.IProductTypeService;
import org.yanfengying.utils.service.impl.BastServerImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yanfengying
 * @since 2022-10-21
 */
@Service
public class ProductTypeServiceImpl extends BastServerImpl<ProductType> implements IProductTypeService {

    @Autowired
    private ProductTypeMapper productTypeMapper;
    @Override
    public List<ProductType> getProTree() {
        List<ProductType> productTypes = productTypeMapper.proTree();
        return productTypes;

    }

    @Override
    public void insert(ProductType productType) {
        //处理path->先新增->拿到对象id,根据id查询父级path->有就父path+/+id->没有就/+id
        super.insert(productType);
        String path = "";
        if(productType.getParent().getId() != null){//有父级产品
            ProductType productType1 = productTypeMapper.loadById(productType.getParent().getId());
            path = productType1.getPath() + "/" +productType.getId();
        }else {
            path = "/" + productType.getId();
        }
        productType.setPath(path);
        super.update(productType);
    }

    @Override
    public void update(ProductType productType) {
        String path = "";
        if(productType.getParent().getId() != null){//有父级产品
            ProductType productType1 = productTypeMapper.loadById(productType.getParent().getId());
            path = productType1.getPath() + "/" +productType.getId();
        }else {
            path = "/" + productType.getId();
        }
        productType.setPath(path);
        super.update(productType);
    }
}
